home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™94 / Talks & Papers / Michael D. Crawford↵ / Word Services SDK 1.0.5 / Writeswell Jr. Source / GenHandlers.c < prev    next >
Text File  |  1993-03-17  |  4KB  |  119 lines

  1. /* GenHandlers.c
  2.  * Do the generic event handling according to Richard Clarke from Dev U.
  3.  * ©1992 Working Software, Inc.
  4.  * This source code is copyrighted.  Permission is granted to use the Word Services
  5.  * portion of the Writeswell Jr. source code in your own programs, but you 
  6.  * may not distribute the Writeswell Jr. word-processor code as a 
  7.  * commercial product.  If you modify the code, please do not call it 
  8.  * Writeswell Jr. (or Writeswell.)  This will ensure that people understand the 
  9.  * program and don’t have to deal with a number of different versions with 
  10.  * who-knows-what going on in the code.
  11.  * 
  12.  * Writeswell Jr. and Writeswell are trademarks of Working Software, Inc.
  13.  * 18 Dec 91 Mike Crawford
  14.  */
  15.  
  16. #include <AppleEvents.h>
  17. #include <AEObjects.h>
  18. #include <AERegistry.h>
  19. #include "TBConstants.h"
  20. #include "TBGlobals.h"
  21. #include "GenHandlers.h"
  22. #include "ObNull.h"
  23. #include "ObWind.h"
  24. #include "ObText.h"
  25. #include "ObOspec.h"
  26. #include "Gripe.h"
  27.  
  28. OSErr InitGenericHandlers( void )
  29. {
  30.     OSErr err;
  31.  
  32.     if ( err = AEInstallEventHandler( kAECoreSuite,
  33.                                 typeWildCard,
  34.                                 (EventHandlerProcPtr)GenericHandler,
  35.                                 0,
  36.                                 false ) ){
  37.         Gripe( "\poapp install failed" );
  38.         return err;
  39.     }
  40.     return err;
  41. }
  42.  
  43. /* In the generic handler, we extract the direct object, resolve it, and identify
  44.  * the type of the finally resolved object (window, text, etc.).  Then we call a routine
  45.  * that handles events for that kind of object.
  46.  *
  47.  * Note that this essentially circumvents the AppleEvent handler dispatch table, and
  48.  * replaces it with our own.  The reason is that it makes it possible to have a separate
  49.  * source file for each kind of data object that will not need to be changed if other
  50.  * sorts of objects are added.  If we have separate event handlers, then when we add
  51.  * an object, we will need to add the code to handle that object to each event handler.
  52.  * In this scheme, if we add support for a new kind of object, we add a source file
  53.  * full of handlers for that object, and add an entry (and a new #include line) in this
  54.  * file.
  55.  *
  56.  * This should make the code more maintainable.  It would be real natural to use an
  57.  * object-oriented language here, where each object would have a standard set of methods
  58.  * for each event that all objects are supposed to handle.
  59.  */
  60.  
  61. pascal OSErr GenericHandler( AppleEvent *theAppleEventPtr,
  62.                                 AppleEvent *replyEventPtr,
  63.                                 long refCon )
  64. {
  65.     AEDesc paramDesc;
  66.     AEDesc tokenDesc;
  67.     OSErr err;
  68.  
  69.     err = AEGetParamDesc( theAppleEventPtr,
  70.                             keyDirectObject,
  71.                             typeObjectSpecifier,
  72.                             ¶mDesc );
  73.  
  74.     if ( err && err != errAEDescNotFound ){        /* It's OK for no parameter to be there */
  75.         /*Gripe( "\pAEGetParamDesc failed" );*/
  76.         return err;
  77.     }
  78.     
  79.     if ( err != errAEDescNotFound ){
  80.         err = AEResolve( ¶mDesc,
  81.                             kAEIDoMinimum,
  82.                             &tokenDesc );
  83.         if ( err ){
  84.             return err;
  85.         }
  86.     }else{
  87.         /* Copy the null param to the token desc */
  88.         err = AEDuplicateDesc( ¶mDesc, &tokenDesc );
  89.         if ( err ){
  90.             Gripe( "\pAEDuplicateDesc failed" );
  91.             return err;
  92.         }
  93.     }
  94.     
  95.     switch ( tokenDesc.descriptorType ){
  96.     
  97.         case typeNull:
  98.             err = DispatchNull( &tokenDesc, theAppleEventPtr, replyEventPtr, refCon );
  99.             break;
  100.         case cWindow:
  101.             err = DispatchWind( &tokenDesc, theAppleEventPtr, replyEventPtr, refCon );
  102.             break;
  103.         case typeTEText:
  104.             err = DispatchTEText( &tokenDesc, theAppleEventPtr, replyEventPtr, refCon );
  105.             break;
  106.         case typeOSpecToken:
  107.             err = DispatchOspec( &tokenDesc, theAppleEventPtr, replyEventPtr, refCon );
  108.             break;
  109.         default:
  110.             Gripe( "\pUnknown object type" );
  111.             return errAEEventNotHandled;
  112.         
  113.     }
  114.  
  115.     err = AEDisposeDesc( &tokenDesc );      /* 1.1.1 MDC fix a memory leak */
  116.                             
  117.     return noErr;
  118. }
  119.